// index.js
Page({
  data: {
    currentWordBookName: '',
    totalWordsInLibrary: 0,    // 词库单词总数
    toLearnInLibrary: 0,       // 词库待学习单词数量
    learnedInLibrary: 0,       // 词库已学习单词数量
    toReviewCount: 0,          // 待复习单词数量
    totalLearnedWords: 0      // 已学习单词总量
  },

  onLoad: function() {
    // 从上一个页面获取到currentwordbook，更新currentWordBookName
    const indexPage = getCurrentPages()[0];
    if (indexPage) {
      this.setData({
        currentWordBookName: indexPage.data.currentwordbook
      })
    }
  },
  // 调用云函数获取学习单词进度统计
  getStudyStatistics: async function() {
    try {
      // 调用云函数wordsFunctions的getStudyStatistics，用于获取学习单词进度统计
      const result = await wx.cloud.callFunction({
        name: 'wordsFunctions', 
        data: {
          type: 'getStudyStatistics'
        }
      })
      if (result.result && result.result.success) {
        this.setData({
          totalWordsInLibrary: result.result.data.totalWordsInLibrary,
          toLearnInLibrary: result.result.data.toLearnInLibrary,
          learnedInLibrary: result.result.data.learnedInLibrary,
          toReviewCount: result.result.data.toReviewCount,
          totalLearnedWords: result.result.data.totalLearnedWords
        })
      } else {
        console.error('获取单词学习进度失败:', result.result.message);
      }
    } catch (error) {
      console.error('调用云函数失败:', error);
    }
  },
  onShow: function() {
    // 获取学习统计数据
    this.getStudyStatistics();
  },

  // 选择词库
  handleAddNew: function() {
    // 跳转到添加新单词
    wx.navigateTo({
      url: '/pages/addwordbank/index'
    })
  },
  
  // 卡片点击事件
  handleCardClick: function(e) {
    const type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: `/pages/wordList/index?type=${type}`,
    });
  }
});